package com.lbj.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.List;
import java.util.Properties;

public class BaseDao {

    private static String drive;
    private static String url;
    private static String username;
    private static String password;

    // 静态代码块，加载类时就已经初始化了
    static {
        InputStream resourceAsStream = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        Properties properties = new Properties();
        try {
            properties.load(resourceAsStream);
            drive = properties.getProperty("drive");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");

            Class.forName(drive);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (resourceAsStream != null) {
                    resourceAsStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 返回连接
     */
    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, username, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }

    /**
     * 执行查询
     *
     * @param connection 数据库连接
     * @param sql        sql语句
     * @param params     sql参数
     * @param resultSet  结果集
     * @return 结果集
     */
    public static ResultSet execute(Connection connection, PreparedStatement statement, ResultSet resultSet, String sql, Object[] params) {
        try {
            // 获取安全的Statement，防止SQL注入
            statement = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                // preparedStatement的下标必须从1开始
                statement.setObject(i + 1, params[i]);
            }
            // 执行查询，获取结果
            resultSet = statement.executeQuery();
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        return resultSet;
    }

    /**
     * 执行修改
     *
     * @param connection 数据库连接
     * @param sql        sql语句
     * @param params     sql参数
     * @return 失败或者成功
     */
    public static int execute(Connection connection, PreparedStatement statement, String sql, Object[] params) {
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject(i + 1, params[i]);
            }
            return preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return -1;
    }

    /**
     * 释放资源
     *
     * @param connection        连接
     * @param preparedStatement 准备参数
     * @param resultSet         查询的结果集
     * @return 成功或失败
     */
    public static boolean release(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
        boolean flag = true;
        if (connection != null) {
            try {
                connection.close();
                connection = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag = false;
            }
        }

        if (preparedStatement != null) {
            try {
                preparedStatement.close();
                preparedStatement = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag = false;
            }
        }

        if (resultSet != null) {
            try {
                resultSet.close();
                resultSet = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag = false;
            }
        }

        return false;
    }
}
